<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="50%">
        <el-image :src="url" fit="cover"></el-image>
      </el-aside>
      <el-main class="main_div">
        <el-form
            :label-position="labelPosition"
            label-width="100px"
            :model="formLabelAlign"
            style="max-width: 90%;height:90%;margin-top: 5%"
        >
          <el-form-item :label="labelName">
            <el-input v-model="formLabelAlign.name" />
          </el-form-item>
          <el-form-item label="password">
            <el-input v-model="formLabelAlign.pwd" type="password" show-password/>
          </el-form-item>
          <el-row class="changAwy" justify="center">
            <el-button @click="ChangLabel('Name')"><el-icon><UserFilled /></el-icon></el-button>
            <el-button @click="ChangLabel('Phone')"><el-icon><PhoneFilled /></el-icon></el-button>
            <el-button @click="ChangLabel('E-mail')"><el-icon><Message /></el-icon></el-button>
          </el-row>
          <el-row justify="space-evenly" style="top: 5%">
            <el-button size="large" style="width: 120px" @click="login">登录</el-button>
            <el-button size="large" style="width: 120px" @click="clear('home')">取消</el-button>
          </el-row>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
// 左边图片加载
const url = 'http://qiniuyun.linter.top/Linter/Vue_img/User/%E7%99%BB%E5%BD%95.png';

// 右边的表单
import {onMounted, reactive, ref} from 'vue'
import {Message, PhoneFilled, UserFilled} from "@element-plus/icons-vue";
import {Login} from "@/api";
import router from "@/router";
import {ElMessage} from "element-plus";
import {loginStatusStore} from '@/stores/counter'

const labelPosition = ref('top')
const formLabelAlign = reactive({name: '', pwd: '',})
const labelName = ref('Name')
const store = loginStatusStore()
function ChangLabel(label) {labelName.value = label}
// 进行登录请求
function login() {
  // 进行登录
  let username = formLabelAlign.name
  let password = formLabelAlign.pwd
  let phone = ''
  let email = ''
  if (labelName.value === 'Phone'){
    username = null
    phone = formLabelAlign.name
  }else if (labelName.value === 'E-mail'){
    username = null
    email = formLabelAlign.name
  }
  const user = {username, password, phone, email}
  console.log(user)
  Login(user).then((res) => {
    console.log(res)
    // 将信息存入浏览器，解决token过期问题
    sessionStorage.setItem('username', username)
    sessionStorage.setItem('password', password)
    sessionStorage.setItem('phone', phone)
    sessionStorage.setItem('email', email)
    let message = res.data.message
    if (message === '登录成功'){
      ElMessage({showClose: true,message: message,type: 'success',})
      router.replace({name: 'home'})
      // 改变登录状态
      const store = loginStatusStore()
      store.logStatus = 1
      sessionStorage.setItem('LoginStore',store.logStatus)
      console.log(store.logStatus)
    }else if (message === '用户不存在！'){
      ElMessage({
        showClose: true,
        message: message + "请重新登录",
        type: 'error',
      })
    }else if (message === '登录信息异常！'){
      ElMessage({
        showClose: true,
        message: "登录信息过时请重新登录",
        type: 'error'
      })
    }
  //   将返回的token存入浏览器
    const token = res.data.data
    window.localStorage.setItem("token", 'Bearer\xa0' + token)
  })
}
// 取消登录并返回首页
function clear(url) {
  router.replace({
    name: url
  })
}
// 使用定时任务，将登录过的用户进行定期登录

</script>

<style scoped>
.common-layout{
  /*border: 1px solid brown;*/
  width: 80%;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
  margin-left: 10%;
  margin-top: 5%;
}
</style>
